package com.dog.basics.service.common.impl;


import cn.hutool.core.lang.Validator;
import com.alipay.api.AlipayApiException;
import com.alipay.api.domain.*;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.response.*;
import com.dog.basics.ijpay.entity.AliPayBean;
import com.dog.basics.service.common.AlipayService;
import com.ijpay.alipay.AliPayApi;
import com.ijpay.alipay.AliPayApiConfig;
import com.ijpay.alipay.AliPayApiConfigKit;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @program: clumsy_security
 * @description:
 * @author: Guo
 * @create: 2019-05-16 11:48
 **/
@Log4j2
@Service
@AllArgsConstructor
public class AlipayServiceImpl implements AlipayService {
    private final AliPayBean aliPayBean;


    /**
     * 订单回调 - 支付宝支付
     *
     * @param request
     * @return guonh 2019年10月14日
     */
    @Override
    @SneakyThrows
    public Map aliPayLowerOrderCallback(HttpServletRequest request) {
        // 获取支付宝POST过来反馈信息
        Map<String, String> params = AliPayApi.toMap(request);
        boolean verifyResult = AlipaySignature.rsaCheckV1(params, aliPayBean.getPublicKey(), "UTF-8", "RSA2");
        Validator.validateTrue(verifyResult, "校验出错!");
        return params;

    }

    public void getApiConfig() throws AlipayApiException {
        AliPayApiConfig aliPayApiConfig = AliPayApiConfig.builder()
                .setAppId(aliPayBean.getAppId())
                .setAppCertPath(aliPayBean.getAppCertPath())
                .setAliPayCertPath(aliPayBean.getAliPayCertPath())
                .setAliPayRootCertPath(aliPayBean.getAliPayRootCertPath())
                .setCharset("UTF-8")
                .setPrivateKey(aliPayBean.getPrivateKey())
                .setAliPayPublicKey(aliPayBean.getPublicKey())
                .setServiceUrl(aliPayBean.getServerUrl())
                .setSignType("RSA2")
                // 普通公钥方式
                //.build();
                // 证书模式
                .buildByCert();
        AliPayApiConfigKit.setThreadLocalAppId(aliPayBean.getAppId()); // 2.1.2 之后的版本，可以不用单独设置
        AliPayApiConfigKit.setThreadLocalAliPayApiConfig(aliPayApiConfig);
    }

    /**
     * APP原生订单发起支付
     *
     * @return guonh 2019年10月14日
     */
    @Override
    @SneakyThrows
    public AlipayTradeAppPayResponse appOrderPay(AlipayTradeAppPayModel model) {
        this.getApiConfig();
        return AliPayApi.appPayToResponse(model, aliPayBean.getBarCodeNotifyUrl());
    }

    /**
     * 支付宝退款
     *
     * @param refundModel
     * @return guonh 2019年10月14日
     */
    @Override
    @SneakyThrows
    public AlipayTradeRefundResponse refund(AlipayTradeRefundModel refundModel) {
        return AliPayApi.tradeRefundToResponse(refundModel);
    }

    /**
     * 企业转账到账户 - 提现
     *
     * @param model
     * @return guonh 2019年10月21日
     */
    @Override
    @SneakyThrows
    public AlipayFundTransToaccountTransferResponse aliPayEntPay(AlipayFundTransToaccountTransferModel model) {
        return AliPayApi.transferToResponse(model);
    }

    /**
     * @Author: guoy
     * @Param: [model, notifyUrl]
     * @Return: com.alipay.api.response.AlipayTradePrecreateResponse
     * @Date: 2019/10/31 11:29
     * 功能描述：扫码支付
     */
    @Override
    @SneakyThrows
    public AlipayTradePrecreateResponse tradePrecreatePayToResponse(AlipayTradePrecreateModel model) {
        return AliPayApi.tradePrecreatePayToResponse(model, aliPayBean.getBarCodeNotifyUrl());
    }


    /**
     * @Author: guoy
     * @Param: [response, model, returnUrl]
     * @Return: java.lang.String
     * @Date: 2019/12/7 11:22
     * 功能描述：网页支付
     */
    @Override
    public Boolean wapPay(HttpServletResponse response, AlipayTradeWapPayModel model, String returnUrl) throws IOException, AlipayApiException {
        AliPayApi.wapPay(response, model, returnUrl, aliPayBean.getBarCodeNotifyUrl());
        return Boolean.TRUE;
    }


    /**
     * @Author: guoy
     * @Param: [alipayTradeCreateModel]
     * @Return: com.alipay.api.response.AlipayTradeCreateResponse
     * @Date: 2019/12/7 11:27
     * 功能描述：统一收单交易创建接口
     */
    @Override
    public AlipayTradeCreateResponse tradeCreateToResponse(AlipayTradeCreateModel alipayTradeCreateModel) throws AlipayApiException {
        return AliPayApi.tradeCreateToResponse(alipayTradeCreateModel, aliPayBean.getBarCodeNotifyUrl());
    }
}
